package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.*;
import com.zb.mapper.ZjWorkSpareMapper;
import com.zb.result.PageUtil;
import com.zb.service.*;
import com.zb.vo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 工单备件表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-10-26
 */
@Service
public class ZjWorkSpareServiceImpl extends ServiceImpl<ZjWorkSpareMapper, ZjWorkSpare> implements ZjWorkSpareService {
    @Resource
    private ZjWorkSpareMapper workSpareMapper;
    @Resource
    private ZjWorkSpareReceiptService workSpareReceiptService;
    @Resource
    private ZjWorkSpareReturnService workSpareReturnService;
    @Resource
    private ZjWorkSpareReturnReceiptSendService workSpareReturnReceiptSendService;

    // 获取工单备件列表
    @Override
    public List<ZjWorkSpare> getList(Long custAccId, WorkSpareSelectVo selectVo) {
        return workSpareMapper.getList(custAccId, selectVo);
    }

    // 获取工单备件列表总记录数
    @Override
    public long getListCount(Long custAccId, WorkSpareSelectVo selectVo) {
        return workSpareMapper.getListCount(custAccId, selectVo);
    }

    // 根据工单ID获取工单详情的工单备件列表
    @Override
    public List<ZjWorkSpare> getOrderDetailWorkSpareList(Long workId, PageUtil pageUtil) {
        return workSpareMapper.getOrderDetailWorkSpareList(workId, pageUtil);
    }
    // 根据工单ID获取工单详情的工单备件列表
    @Override
    public List<ZjWorkSpare> getOrderDetailWorkSpareList(Long workId) {
        return workSpareMapper.getOrderDetailWorkSpareLists(workId);
    }

    // 根据工单ID获取工单详情的工单备件列表总记录数
    @Override
    public long getOrderDetailWorkSpareListCount(Long workId) {
        return workSpareMapper.getOrderDetailWorkSpareListCount(workId);
    }

    // 获取审核前的工单备件信息
    @Override
    public WorkSpareDetailVo getBeforeCheckOrderSpareDetail(Long id) {
        ZjWorkSpare workSpare = this.getById(id);
        Assert.isTrue(null != workSpare, "工单备件ID参数["+ id +"]的备件数据不存在");

        WorkSpareDetailVo vo = new WorkSpareDetailVo();
        BeanUtil.copyProperties(workSpare, vo);
        vo.setCreateTime(workSpare.getGmtCreate());
        vo.setUpdateTime(workSpare.getGmtUpdate());
        ZjWorkSpareReceipt receipt = workSpareReceiptService.getById(id);
        vo.setReceiptAddrType(receipt.getReceiptAddrType());
        vo.setUserOrNetworkId(receipt.getUserOrNetworkId());
        vo.setReceiptName(receipt.getReceiptName());
        vo.setReceiptPhone(receipt.getReceiptPhone());
        vo.setReceiptProvince(receipt.getReceiptProvince());
        vo.setReceiptCity(receipt.getReceiptCity());
        vo.setReceiptArea(receipt.getReceiptArea());
        vo.setReceiptTown(receipt.getReceiptTown());
        vo.setReceiptAddress(receipt.getReceiptAddress());
        return vo;
    }

    // 根据服务商ID获取与工单备件关联的服务商ID
    @Override
    public List<Long> getRelationByNetworkIds(List<Long> networkIds) {
        if (CollectionUtil.isEmpty(networkIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkSpare> wrapper = new QueryWrapper<>();
        wrapper.select("network_id");
        wrapper.in("network_id", networkIds);
        return this.list(wrapper).stream().map(ZjWorkSpare::getNetworkId).distinct().toList();
    }

    // 获取服务商详情之工单配件列表
    @Override
    public List<ZjWorkSpare> getNetworkDetailOrderSpareList(Long networkId, PageUtil pageUtil) {
        return workSpareMapper.getNetworkDetailOrderSpareList(networkId, pageUtil);
    }

    // 获取服务商详情之工单配件列表总记录数
    @Override
    public long getNetworkDetailOrderSpareListCount(Long networkId) {
        return workSpareMapper.getNetworkDetailOrderSpareListCount(networkId);
    }

    // 获取商家详情之工单配件列表
    @Override
    public List<ZjWorkSpare> getFactoryDetailOrderSpareList(Long factoryId, PageUtil pageUtil) {
        return workSpareMapper.getFactoryDetailOrderSpareList(factoryId, pageUtil);
    }

    // 获取商家详情之工单配件列表总记录数
    @Override
    public long getFactoryDetailOrderSpareListCount(Long factoryId) {
        return workSpareMapper.getFactoryDetailOrderSpareListCount(factoryId);
    }
}
